package com.netease.xone.itemview;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;

import media.MediaPlayerWrapper;
import media.MediaPlayerWrapper.MediaListener;
import media.MediaPlayerWrapper.PlayStatus;
import protocol.meta.AppAudio;
import protocol.meta.AppInfo;
import protocol.meta.AppInfoDetail;
import Utils.StackTraceUtil;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.netease.log.NTLog;
import com.netease.xone.R;


public class RenderInfoAudio implements IRenderInfoBase, OnClickListener, MediaListener{
    private static final String TAG = RenderInfoAudio.class.getSimpleName();

    private static int mDefaultMaxDuration = 90 * 1000; // 默认90秒
    private static int mMinWidth;
    private static int mMaxWidth;
    
    private String mNickname;
    
    private View mRoot;
    private TextView mAnimTextView;
    private AnimationDrawable mAnimationDrawable;
    private TextView mTextView;
    private List<AppAudio> mList;
    
    public RenderInfoAudio(View v) {
        mRoot = v;
        
        mAnimTextView = (TextView)v.findViewById(R.id.item_view_info_audio_anim);
        mAnimTextView.setOnClickListener(this);
        Drawable[] drawables = null;
        drawables = mAnimTextView.getCompoundDrawables();
        mAnimationDrawable = (AnimationDrawable)drawables[0];
        mTextView = (TextView)v.findViewById(R.id.item_view_info_audio_time);
    }

    @Override
    public void renderView(Context context, AppInfo appInfo) {
        renderView(context, appInfo.getAudios());
        if (AppInfo.TYPE_AUDIO == appInfo.getInfoType()) {
            mNickname = appInfo.getUser().getNickname();
        } else {
            mNickname = null;
        }
    }
    
    @Override
    public void renderView(Context context, AppInfoDetail appInfoDetail) {
        renderView(context, appInfoDetail.getAudios());
    }

    public void renderView(Context context, List<AppAudio> list) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        mList = list;
        if (null != list && 0 < list.size()) {
            mRoot.setVisibility(View.VISIBLE);
        } else {
            mRoot.setVisibility(View.GONE);
            return;
        }
        
        if (null != mTextView) {
            mTextView.setText(getStringByDuration(list.get(0).getDuration()));
        }
        ViewGroup.LayoutParams params = mAnimTextView.getLayoutParams();
        params.width = getWidthByDuration(context, list.get(0).getDuration());
        mAnimTextView.setLayoutParams(params);
        /**
         * 判断现在播放的音乐是否时自己，如果是要启动播放动画
         */
        MediaPlayerWrapper mediaPlayer = MediaPlayerWrapper.getInstance();
        if (null != mList.get(0).getUrl() &&
                mList.get(0).getUrl().equals(mediaPlayer.getFilePath())) {
            if (PlayStatus.PLAYING == mediaPlayer.getPlayStatus()) {
                mAnimationDrawable.stop();
                mAnimationDrawable.start();
                mAnimTextView.setSelected(true);
            }
        }
        mediaPlayer.registerMediaListener(this);
    }
    
    @Override
    public void onClick(View v) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        MediaPlayerWrapper mediaPlayer = MediaPlayerWrapper.getInstance();
        if (v.isSelected()) {
            v.setSelected(false);
            mediaPlayer.pause();
        } else {
            v.setSelected(true);
            if (null != mList.get(0).getUrl() &&
                    !mList.get(0).getUrl().equals(mediaPlayer.getFilePath())) {
                mediaPlayer.play(mList.get(0).getUrl());
                mediaPlayer.setName(mNickname);
            } else {
                mediaPlayer.play();
            }
        }
    }
    
    public static int getWidthByDuration(Context context, int duration) {
        int width = 0;
        
        mMinWidth = context.getResources().getDimensionPixelSize(R.dimen.info_size_69);
        mMaxWidth = context.getResources().getDimensionPixelSize(R.dimen.info_size_345);
        
        if (duration >= mDefaultMaxDuration) {
            width = mMaxWidth;
        } else {
            width = mMinWidth + ((mMaxWidth - mMinWidth) * duration) / mDefaultMaxDuration;
        }
        NTLog.d(TAG, StackTraceUtil.getMethodName() + " width=" + width);
        return width;
    }
    
    public static String getStringByDuration(int duration) {
        String value = null;

        if (duration <= 0) {
            value = "0\"";
            return value;
        }
        
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        formatter.setTimeZone(TimeZone.getTimeZone("UTC +08:00")); 
        value = formatter.format(duration);
        NTLog.d(TAG, StackTraceUtil.getMethodName() + " time=" + value);
        String[] splits =value.split(":");
        if (splits[0].endsWith("00")) {
            if (splits[1].endsWith("00")) {
                value = splits[2] + "\"";
            } else {
                value = splits[1] + "'" + splits[2] + "\"";
            }
        } else {
            value = splits[0] + ":" + splits[1] + "'" + splits[2] + "\"";
        }
        
        return value;
    }

    private boolean isMyself() {
        boolean bRes = false;
        if (null != mList.get(0).getUrl() &&
                mList.get(0).getUrl().equals(MediaPlayerWrapper.getInstance().getFilePath())) {
            bRes = true;
        }
        return bRes;
    }
    
    @Override
    public void onMediaPlay() {
        if (isMyself()) {
            mAnimationDrawable.stop();
            mAnimationDrawable.start();
        }
    }

    @Override
    public void onMediaPause() {
        if (isMyself()) {
            mAnimationDrawable.stop();
        }
    }

    @Override
    public void onMediaRelease() {
        MediaPlayerWrapper.getInstance().registerMediaListener(this);
        mAnimationDrawable.stop();
        mAnimTextView.setSelected(false);
    }

    @Override
    public void onMediaCompletion() {
        MediaPlayerWrapper.getInstance().release();
    }
}
